package cn.sypt.yuhao.dao;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.Session;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.itcast.jdbc.TxQueryRunner;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
import cn.sypt.yuhao.domain.Acategory;
import cn.sypt.yuhao.domain.Bcategory;

public class AcategoryDao {
	private QueryRunner qr=new TxQueryRunner();
	private BcategoryDao bd=new BcategoryDao();
	public List<Acategory> findByfid(String fid){
		String sql="select * from acategory where fid=?";
		try {
			List<Acategory> scategoryList=qr.query(sql, new BeanListHandler<Acategory>(Acategory.class),fid);
			for (int i = 0; i < scategoryList.size(); i++) {
				String aid=scategoryList.get(i).getAid();
				List<Bcategory> bcategory=bd.findByaid(aid);
				scategoryList.get(i).setBcategory(bcategory);
			}
			return scategoryList;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	public void testmail(){
		String host = "smtp.163.com";//服务器主机名
		String name = "experimentlearning@163.com";//登录名
		String pass = "learning2016";//登录密码
		Session session = MailUtils.createSession(host, name, pass);
		
		/*
		 * 创建Mail对象
		 */
		String from = "experimentlearning@163.com";
		String to = "2990654397@qq.com";//发送对象，需要用户的邮箱....自己根据需要填写
		String subject ="来自高校实验学习平台的激活邮件";//邮件的主题....自己根据需要填写
		String content ="欢迎您激活邮件http://localhost:8080/sypt/CategoryServlet?method=findAll    激活该邮件";//邮件的内容....自己根据需要填写
		Mail mail = new Mail(from, to, subject, content);
		/*
		 * 发送邮件
		 */
		try {
			MailUtils.send(session, mail);
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
}
